package com.hjm.common.utils.request;
import com.alibaba.fastjson.JSONObject;
import com.hjm.common.utils.AESUtil;
import com.hjm.common.utils.DigestUtils;
import com.hjm.common.utils.ImageBinaryUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:   要素认证工具
 * @Version:2.0
 */
@Component
public class EssentialFactors {
    private final static String reqDate = DateUtils.getDateFormatS(new Date());
    private final static String reqTime =  DateUtils.getSysHMS();
   /* private final static String merId = "42233911";
    private final static String busiMerId = "42233911";
    private final static String ldgsurl="http://10.10.55.75:8088/commAuth/800004";
    private final static String dxurl="http://10.10.55.73:37999/smsParserService/downsms";
    private final static String ldsysgsqjurl="http://10.10.55.75:8088/commAuth/800007";*/
    private static final Logger esLogger = LogManager.getLogger("resultPack");
    private static final Logger bxLogger = LogManager.getLogger("exceptionPack");
    private static final String AES_KEY = "8889c832ebb0f90772e6036b02626411";// 鉴权互通请求信息加密key

    /**
     * @Author: dongjb
     * @Date: 2019/5/21 16:38
     * @Description:  二要素身份认证
     * @Version: 1.0
     */


   /* public static void main(String[] args) {
        Map<String,Object> map=new HashMap<>();
        map.put("ScreenName","路冉");
        map.put("Idnumber","130627199005231215");
        System.out.println(twoElements(map));
    }*/
    public static Map twoElements(Map cardBag){
       /* System.out.println("第二步：" + cardBag.toString());
        String rpid = ("UPLM"+System.currentTimeMillis()).substring(0, 15);
        Map reqmap = new HashMap();
        reqmap.put("reqDate",reqDate);
        reqmap.put("rpid",rpid);
        reqmap.put("reqTime",reqTime);
        reqmap.put("merId",AutoLoadData.merId);
        reqmap.put("busiMerId",AutoLoadData.busiMerId);
        reqmap.put("productId","P15X00RZ");
        reqmap.put("accountName",cardBag.get("ScreenName"));
        reqmap.put("bankIdentityCode", TestDesUtils.desEncrypt(cardBag.get("Idnumber").toString()));
        System.out.println("第三步：" + reqmap.toString());
        JSONObject jsonObject = HttpClientUtils.sendHttpPostForm(AutoLoadData.ldysurl,reqmap);
        System.out.println("第四步：" + jsonObject.toString());
        String xml = jsonObject.get("responseVal").toString();
        Map map = XmlUtil.doXMLParse(xml);
        if("0000".equals(map.get("retCode"))){
            map.put("rpid",rpid);
            map.put("status","1");
        }else{
            map.put("status","0");
        }
        return map;*/
        try{
            Map reqmap = new HashMap();
            //reqmap.put("merno",cardBag.get("Merno"));//商户号
            reqmap.put("idnumber", cardBag.get("Idnumber").toString());//身份证号
            reqmap.put("screenName", cardBag.get("ScreenName"));//客户请求流水单号
            reqmap.put("source","5");//用户来源
            reqmap.put("sign", DigestUtils.encodeShaHexStr(reqmap));//签名
            Map map = new HashMap();
            JSONObject jsonObj = new JSONObject(reqmap);
            esLogger.info("二要素请求:{}",jsonObj.toString());
            map.put("data", AESUtil.encrypt(jsonObj.toString(),AES_KEY));
            //AppData
            JSONObject jsonObject =  HttpClientUtils.sendHttpPostForm(AutoLoadData.ldysurl+"/ess/twoFactor.do",map);
            Map toMap = new HashMap();
            if("200".equals(jsonObject.getString("responseCode"))){
                Map maps = JSONObject.parseObject(jsonObject.getString("responseVal"), Map.class);
                String ss = AESUtil.decrypt(maps.get("data").toString(), AES_KEY);
                esLogger.info("二要素响应:{}",ss);
                Map reqMap = JSONObject.parseObject(ss, Map.class);
                reqMap = JSONObject.parseObject(reqMap.get("resultData").toString(), Map.class);
                return reqMap;
            }else{
                toMap.put("status","1");
                toMap.put("msg","系统错误，请稍后再试！");
                return toMap;
            }
        }catch (Exception e){
            bxLogger.info("二要素身份认证",e);
            e.printStackTrace();
            return null;
        }
    }
    /**
     * @Author: dongjb
     * @Date: 2019/5/21 16:38
     * @Description:  三要素认证
     * @Version: 1.0
     */
    public static Map threeElements(Map cardBag){
/*
        String rpid = ("UPLM"+System.currentTimeMillis()).substring(0, 15);
        Map reqmap = new HashMap();
        reqmap.put("reqDate",reqDate);
        reqmap.put("rpid",rpid);
        reqmap.put("reqTime",reqTime);
        reqmap.put("merId",AutoLoadData.merId);
        reqmap.put("busiMerId",AutoLoadData.busiMerId);
        reqmap.put("productId","P15X00RZ");
        reqmap.put("bankIdentityType","1");
        reqmap.put("bankAccount", TestDesUtils.desEncrypt(cardBag.get("ScreenNum").toString()));
        reqmap.put("accountName",cardBag.get("ScreenName"));
        reqmap.put("bankIdentityCode", TestDesUtils.desEncrypt(cardBag.get("Idnumber").toString()));
        JSONObject jsonObject = HttpClientUtils.sendHttpPostForm(AutoLoadData.ldysurl,reqmap);
        String xml = jsonObject.get("responseVal").toString();
        Map map = XmlUtil.doXMLParse(xml);
        if("0000".equals(map.get("retCode"))){
            map.put("rpid",rpid);
            map.put("status","1");
        }else{
            map.put("status","0");
        }
        return map;*/
        try{
            Map reqmap = new HashMap();
            //reqmap.put("merno",cardBag.get("Merno"));//商户号
            reqmap.put("idnumber", cardBag.get("Idnumber").toString());//身份证号
            reqmap.put("screenName", cardBag.get("ScreenName"));//客户请求流水单号
            reqmap.put("screenNum",cardBag.get("ScreenNum").toString());//银行卡号
            reqmap.put("source","5");//用户来源
            reqmap.put("sign",DigestUtils.encodeShaHexStr(reqmap));//签名
            Map map = new HashMap();
            JSONObject jsonObj = new JSONObject(reqmap);
            esLogger.info("三要素请求:{}",jsonObj.toString());
            map.put("data", AESUtil.encrypt(jsonObj.toString(),AES_KEY));
            JSONObject jsonObject =  HttpClientUtils.sendHttpPostForm(AutoLoadData.ldysurl+"/ess/threeFactor.do",map);
            Map toMap = new HashMap();
            if("200".equals(jsonObject.getString("responseCode"))){
                Map maps = JSONObject.parseObject(jsonObject.getString("responseVal"), Map.class);
                String ss = AESUtil.decrypt(maps.get("data").toString(), AES_KEY);
                esLogger.info("三要素响应:{}",ss);
                if (ss==null){
                    toMap.put("status","1");
                    toMap.put("msg","暂无响应,请稍后再试！");
                    return toMap;
                }
                Map reqMap = JSONObject.parseObject(ss, Map.class);
                reqMap = JSONObject.parseObject(reqMap.get("resultData").toString(), Map.class);
                return reqMap;
            }else{
                toMap.put("status","1");
                toMap.put("msg","系统错误，请稍后再试！");
                return toMap;
            }
        }catch (Exception e){
            bxLogger.info("三要素认证",e);
            e.printStackTrace();
            return null;
        }
    }
    /**
     * @Author: dongjb
     * @Date: 2019/5/21 16:38
     * @Description:  四要素认证
     * @Version: 1.0
     */
    public static Map fourElements(Map cardBag){
       /* String rpid = ("UPLM"+System.currentTimeMillis()).substring(0, 15);
        Map reqmap = new HashMap();
        reqmap.put("reqDate",reqDate);
        reqmap.put("rpid",rpid);
        reqmap.put("reqTime",reqTime);
        reqmap.put("merId",AutoLoadData.merId);
        reqmap.put("busiMerId",AutoLoadData.busiMerId);
        reqmap.put("productId","P15X00RZ");
        reqmap.put("bankIdentityType","1");
        reqmap.put("bankAccount", TestDesUtils.desEncrypt(cardBag.get("ScreenNum").toString()));
        reqmap.put("accountName",cardBag.get("ScreenName"));
        reqmap.put("bankIdentityCode", TestDesUtils.desEncrypt(cardBag.get("Idnumber").toString()));
        reqmap.put("bankMobileId",cardBag.get("Resphone"));
        System.out.println("四要素地址："+AutoLoadData.ldysurl);
        JSONObject jsonObject = HttpClientUtils.sendHttpPostForm(AutoLoadData.ldysurl,reqmap);
        String xml = jsonObject.get("responseVal").toString();
        Map map = XmlUtil.doXMLParse(xml);
        if("0000".equals(map.get("retCode"))){
            map.put("rpid",rpid);
            map.put("status","1");
        }else{
            map.put("status","0");
        }
        return map;*/
        try{
            Map reqmap = new HashMap();
            //reqmap.put("merno",cardBag.get("Merno"));//商户号
            reqmap.put("idnumber", cardBag.get("Idnumber").toString());//身份证号
            reqmap.put("screenName", cardBag.get("ScreenName"));//客户请求流水单号
            reqmap.put("screenNum",cardBag.get("ScreenNum").toString());//银行卡号
            reqmap.put("resphone",cardBag.get("Resphone"));//预留手机号
            reqmap.put("source","5");//用户来源
            reqmap.put("sign",DigestUtils.encodeShaHexStr(reqmap));//签名
            Map map = new HashMap();
            JSONObject jsonObj = new JSONObject(reqmap);
            esLogger.info("四要素请求:{}",jsonObj.toString());
            map.put("data", AESUtil.encrypt(jsonObj.toString(),AES_KEY));
            JSONObject jsonObject =  HttpClientUtils.sendHttpPostForm(AutoLoadData.ldysurl+"/ess/fourFactor.do",map);
            Map toMap = new HashMap();
            if("200".equals(jsonObject.getString("responseCode"))){
                Map maps = JSONObject.parseObject(jsonObject.getString("responseVal"), Map.class);
                String ss = AESUtil.decrypt(maps.get("data").toString(), AES_KEY);
                esLogger.info("四要素响应:{}",ss);
                Map reqMap = JSONObject.parseObject(ss, Map.class);
                reqMap = JSONObject.parseObject(reqMap.get("resultData").toString(), Map.class);
                return reqMap;
            }else{
                toMap.put("status","1");
                toMap.put("msg","系统错误，请稍后再试！");
                return toMap;
            }
        }catch (Exception e){
            bxLogger.info("四要素认证",e);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @Author 野猪佩奇
     * @Description //工商权建接口
     * @Date 18:15 2019/5/25
     * @Param [map]
     * @return java.util.Map
     **/
    public static Map businessInfo(String lisence){
        String rpid = ("UPLM"+System.currentTimeMillis()).substring(0, 15);
        Map reqmap = new HashMap();
        reqmap.put("reqDate",reqDate);
        reqmap.put("rpid",rpid);
        reqmap.put("reqTime",reqTime);
        reqmap.put("merId",AutoLoadData.merId);
        reqmap.put("busiMerId",AutoLoadData.busiMerId);
        reqmap.put("productId","P15000R1");
        reqmap.put("entInfo",lisence); //91110108717743469K
        JSONObject jsonObject = HttpClientUtils.sendHttpPostForm(AutoLoadData.ldgsurl,reqmap);
        String xml = jsonObject.get("responseVal").toString();
        Map map = XmlUtil.doXMLParse(xml);
        if("0000".equals(map.get("retCode"))){
            map.put("rpid",rpid);
        }
        return map;
    }

    public static Map lDLive(Map cardBag, File file, String merOrderId){
        try{
            String rpid = ("UPLM"+System.currentTimeMillis()).substring(0, 15);
            Map reqmap = new HashMap();
            reqmap.put("reqDate",reqDate);
            reqmap.put("rpid",rpid);
            reqmap.put("reqTime",reqTime);
            reqmap.put("merId",AutoLoadData.merId);
            reqmap.put("busiMerId",AutoLoadData.busiMerId);
            reqmap.put("productId","P15010I0");
            reqmap.put("merOrderId",merOrderId);
            reqmap.put("accountName",cardBag.get("legal_name")); //姓名
            reqmap.put("bankIdentityCode",TestDesUtils.desEncrypt(cardBag.get("legal_cerno").toString())); //身份证号
            reqmap.put("image", ImageBinaryUtil.getImageBinary(file));
            String xml = HttpClientUtil.doPost(AutoLoadData.ldfaceidurl,reqmap);
            return XmlUtil.doXMLParse(xml);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static Map fourBusinessinformation(Map map1){
        String rpid = ("UPLM"+System.currentTimeMillis()).substring(0, 15);
        Map reqmap = new HashMap();
        reqmap.put("reqDate",reqDate);
        reqmap.put("rpid",rpid);
        reqmap.put("reqTime",reqTime);
        reqmap.put("merId",AutoLoadData.merId);
        reqmap.put("busiMerId",AutoLoadData.busiMerId);
        reqmap.put("productId","P15000I9");
        reqmap.put("orgType","20");
        reqmap.put("orgNo",map1.get("social_redit_code"));
        reqmap.put("entName",map1.get("mer_name"));
        reqmap.put("accountName",map1.get("legal_name"));
        reqmap.put("bankIdentityCode",TestDesUtils.desEncrypt(map1.get("legal_cerno").toString()));
        reqmap.put("bankIdentityType","1");//身
        // 份证
        System.out.println("工商四要素："+AutoLoadData.ldsysgsqjurl);
        JSONObject jsonObject = HttpClientUtils.sendHttpPostForm(AutoLoadData.ldsysgsqjurl,reqmap);
        String xml = jsonObject.get("responseVal").toString();
        Map map = XmlUtil.doXMLParse(xml);
        if("0000".equals(map.get("retCode"))){
            map.put("rpid",rpid);
            map.put("status","1");
        }else{
            map.put("status","0");
        }
        System.out.println(map.toString());
        return map;
    }

    /**
     * 短信发送
     * @param maps
     * @return
     */
    public static Map smsCode(Map<String,String> maps) throws IOException, DocumentException {
        Map<String, String> respMap = new HashMap<>();
        String rpid = ("UPLM" + System.currentTimeMillis()).substring(0, 15);
        String str = "<map>" +
                "  <entry>" +
                "    <string>calling</string>" +
                "    <string>" + maps.get("phone") + "</string>" +
                "  </entry>" +
                "  <entry>" +
                "    <string>rpid</string>" +
                "    <string>" + rpid + "</string>" +
                "  </entry>" +
                "  <entry>" +
                "    <string>funCode</string>" +
                "    <string>" + AutoLoadData.fun_code + "</string>" +
                "  </entry>" +
                "  <entry>" +
                "    <string>Memo</string>" +
                "    <string>" + maps.get("Memo") + "</string>" +
                "  </entry>" +
                "  <entry>" +
                "    <string>retCode</string>" +
                "    <string>" + AutoLoadData.ret_code + "</string>" +
                "  </entry>" +
                "  <entry>" +
                "    <string>reqDate</string>" +
                "    <string>" + reqDate + "</string>" +
                "  </entry>" +
                "  <entry>" +
                "    <string>reqTime</string>" +
                "    <string>" + reqTime + "</string>" +
                "  </entry>" +
                "</map>";
        JSONObject jsonObject = HttpClientUtils.sendHttpPostJson(AutoLoadData.dxurl, str);

        if ("200".equals(jsonObject.get("responseCode").toString())) {
            String resp = BasePF.formatXml(jsonObject.get("responseVal").toString());
            respMap = SaopXml.paresXml(resp);
            if ("0000".equals(respMap.get("retCode"))) {
                respMap.put("status", "2");
            } else {
                respMap.put("status", "1");
            }
        } else {
            respMap.put("status", "1");
        }
        return respMap;
    }

}


